package com.ziyucoding.springboot.labs.lab10.seatdemo.tccscfeign.transactionrecordservice.service.impl;

import com.ziyucoding.springboot.labs.lab10.seatdemo.tccscfeign.transactionrecordservice.dao.TransactionalRecordDao;
import com.ziyucoding.springboot.labs.lab10.seatdemo.tccscfeign.transactionrecordservice.dataobject.TransactionalRecordDO;
import com.ziyucoding.springboot.labs.lab10.seatdemo.tccscfeign.transactionrecordservice.service.TransactionalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 事务记录服务实现
 *
 * @author ziyucoding
 * @date 2022-11-06
 **/
@Service
public class TransactionalRecordServiceImpl implements TransactionalRecordService {

    @Autowired
    private TransactionalRecordDao transactionalRecordDao;

    @Override
    @Transactional
    public Integer saveTransactionalRecord(TransactionalRecordDO item) {
        Integer count = 0;
        // 保证幂等性
        String xid = item.getXid();
        TransactionalRecordDO result = transactionalRecordDao.findByXid(xid);
        if (Objects.nonNull(result))
            count = 1;
        else {
            count = transactionalRecordDao.saveTransactionalRecord(item);
        }
        return count;
    }

    @Override
    public TransactionalRecordDO findByXid(String xid) {
        return transactionalRecordDao.findByXid(xid);
    }
}
